package com.jade.editor.domain.parts;

import java.io.File;

import org.eclipse.swt.SWT;
import org.eclipse.swt.custom.ScrolledComposite;
import org.eclipse.swt.events.ControlAdapter;
import org.eclipse.swt.events.ControlEvent;
import org.eclipse.swt.events.MouseAdapter;
import org.eclipse.swt.events.MouseEvent;
import org.eclipse.swt.events.PaintEvent;
import org.eclipse.swt.events.PaintListener;
import org.eclipse.swt.graphics.Image;
import org.eclipse.swt.graphics.Point;
import org.eclipse.swt.graphics.RGB;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.wb.swt.SWTResourceManager;

import com.jade.editor.domain.ani.Anisotropy;

public class DrawArea {
	
	public static final int METRIC_PX = 0;
	public static final int METRIC_MM = 1;
	public static final int METRIC_CM = 2;
	
	public static final int BACKGROUND_NONE = 0;
	public static final int BACKGROUND_COLOR = 1;
	public static final int BACKGROUND_IMAGE = 2;
	
	private Composite _parent;
	private Composite _drawArea;
	private Composite _drawLayer;
	private ScrolledComposite _scrollArea;
	private Grid _grid = new Grid(37);
	private Ruler _ruler = new Ruler(METRIC_PX);
	private boolean _rulerVisible = false;
	private int _bgStatus = BACKGROUND_NONE;
	private File _bgImageFile = null;
	private Image _bgImage = null;
	private Point _imageLocation = new Point(0, 0);
	private boolean _showBGImage = false;
	private Anisotropy _ani = null;
//	private NodeCollector _ncollector = null;
//	private ObjectModel _objModel = null;
//	private ObjectEditor _editor = null;
	
	public void setDrawAreaSize(Point size){
		_drawArea.setSize(size);
		//_drawLayer.setSize(size);
	}
	
//	public NodeCollector getNodeCollector(){
//		return _ncollector;
//	}
//	
//	public void setObjectModel(ObjectModel objModel) {
//		_objModel = objModel;
//	}
//	
//	public void setEditor(ObjectEditor editor){
//		_editor = editor;
//	}
	
	public Anisotropy getAnisotropy(){
		return _ani;
	}
	
	public void setAnisotropy(Anisotropy ani){
		_ani = ani;
		_drawArea.redraw();
	}
	
	public Composite getDrawAreaComposite(){
		return _drawArea;
	}
	
	public void showBGImage(boolean b){
		_showBGImage = b;
		_drawArea.redraw();
	}
	
	public void setBGImageLocation(Point location){
		_imageLocation = location;
	}
	
	public Point getBGImageLocation(){
		return _imageLocation;
	}
	
	public void setBGColor(RGB color){
		_drawArea.setBackground(SWTResourceManager.getColor(color));
	}
	
	public RGB getBGColor(){
		return _drawArea.getBackground().getRGB();
	}
	
	public void setBGImage(File bgImage){
		_bgImageFile = bgImage;
		_bgImage = new Image(_drawArea.getDisplay(), _bgImageFile.toString());
		_drawArea.redraw();
	}
	
	public File getBGImage(){
		return _bgImageFile;
	}
	
	public Point getDrawAreaSize(){
		return _drawArea.getSize();
	}
	
	public int getBGStatus(){
		return _bgStatus;
	}
	
	public void setBGStatus(int status){
		_bgStatus = status;
		if (_bgStatus == DrawArea.BACKGROUND_NONE){
			_drawArea.setBackground(SWTResourceManager.getColor(240,240,240));
		}
	}
	
	public int getGridDistanse(){
		return _grid.getDistanse();
	}
	
	public void setGridDistanse(int dist){
		_grid.setDistanse(dist);
	}
	
	public RGB getGridColor(){
		return _grid.getColor();
	}
	
	public void setGridColor(RGB color){
		_grid.setColor(color);
	}
	
	public void showGrid(boolean b){
		_grid.showGrid(b);
	}
	
	public DrawArea(Composite parent/*, ObjectModel objModel, ObjectEditor editor*/){
		_parent = parent;
//		_objModel = objModel;
//		_editor = editor;
	}
	
	public void setParent(Composite parent){
		_parent = parent;
	}
	
	public void setRulerVisible(boolean b){
		_ruler.setVisible(b);
		_rulerVisible = b;
		if (b) _drawArea.setLocation(20, 20);
		else _drawArea.setLocation(0, 0);
		
		int xdraw = _scrollArea.getBounds().width;
		int ydraw = _scrollArea.getBounds().height;
		if (_drawArea.getBounds().width >= xdraw)
			xdraw = _rulerVisible ? _drawArea.getBounds().width + 20 : _drawArea.getBounds().width;
		if (_drawArea.getBounds().height >= ydraw)
			ydraw = _rulerVisible ?  _drawArea.getBounds().height + 20 : _drawArea.getBounds().height;
		
		
		_drawLayer.setBounds(0, 0, xdraw, ydraw);
	}
	
	
	public void initArea(){
		

		_scrollArea = new ScrolledComposite(_parent, SWT.H_SCROLL | SWT.V_SCROLL);
		_scrollArea.setBounds(26, 0, 600, 600);
		_scrollArea.setBackground(SWTResourceManager.getColor(SWT.COLOR_TITLE_INACTIVE_BACKGROUND_GRADIENT));
		_scrollArea.setMinSize(600,600);
		
		_drawLayer = new Composite(_scrollArea, SWT.NONE);
		_drawLayer.setBounds(0, 0, 600, 600);
		_drawLayer.setBackground(SWTResourceManager.getColor(SWT.COLOR_TITLE_INACTIVE_BACKGROUND_GRADIENT));
		_scrollArea.setContent(_drawLayer);
		
		
		_drawArea = new Composite(_drawLayer, SWT.NONE);
		_drawArea.setBackground(SWTResourceManager.getColor(240,240,240));
		
		int xsize = 500;
		int ysize = 500;
//		if(_objModel.getDistance() != null && _objModel.getDistance().x > 100)
//			xsize = _objModel.getDistance().x;
//		if(_objModel.getDistance() != null && _objModel.getDistance().y > 100)
//			ysize = _objModel.getDistance().y;
		_drawArea.setBounds(20, 20, xsize, ysize);
		
		initObjectData();
		_drawArea.addMouseListener(new MouseAdapter() {
			@Override
			public void mouseUp(MouseEvent e) {
//				_ncollector.addNode(e.x, e.y);
			}
		});
		
		
		_parent.addControlListener(new ControlAdapter() {
		@Override
		public void controlResized(final ControlEvent e) {
			_scrollArea.setBounds(26, 0, _parent.getSize().x - 26, _parent.getSize().y - 18);
			_scrollArea.setMinSize(_scrollArea.getBounds().width, _scrollArea.getBounds().height);
			
			int xdraw = _scrollArea.getBounds().width;
			int ydraw = _scrollArea.getBounds().height;
			if (_drawArea.getBounds().width >= xdraw)
				xdraw = _rulerVisible ? _drawArea.getBounds().width + 20 : _drawArea.getBounds().width;
			if (_drawArea.getBounds().height >= ydraw)
				ydraw = _rulerVisible ?  _drawArea.getBounds().height + 20 : _drawArea.getBounds().height;
			
			
			_drawLayer.setBounds(0, 0, xdraw, ydraw);
			if(_rulerVisible)
				_drawArea.setLocation(20, 20);
			else
				_drawArea.setLocation(0, 0);
			}
		});
		
		_drawArea.addControlListener(new ControlAdapter() {
		@Override
		public void controlResized(final ControlEvent e) {
			_scrollArea.setBounds(26, 0, _parent.getSize().x - 26, _parent.getSize().y - 18);
			_scrollArea.setMinSize(_scrollArea.getBounds().width, _scrollArea.getBounds().height);
			
			int xdraw = _scrollArea.getBounds().width;
			int ydraw = _scrollArea.getBounds().height;
			if (_drawArea.getBounds().width >= xdraw)
				xdraw = _rulerVisible ? _drawArea.getBounds().width + 20 : _drawArea.getBounds().width;
			if (_drawArea.getBounds().height >= ydraw)
				ydraw = _rulerVisible ?  _drawArea.getBounds().height + 20 : _drawArea.getBounds().height;
			
			
			_drawLayer.setBounds(0, 0, xdraw, ydraw);
			if(_rulerVisible)
				_drawArea.setLocation(20, 20);
			else
				_drawArea.setLocation(0, 0);
			}
		});
		
		
//		TODO scroll listeners
//		SelectionListener listenerH = new SelectionAdapter() {
//			public void widgetSelected(SelectionEvent e) {
//				leftRuler.setLocation(scrollArea.getOrigin().x, 20);
//				rulerNull.setLocation(scrollArea.getOrigin().x, rulerNull.getLocation().y);
//			}
//		};
//		
//		scrollArea.getHorizontalBar().addSelectionListener(listenerH);
//		
//		SelectionListener listenerV = new SelectionAdapter() {
//			public void widgetSelected(SelectionEvent e) {
//				upperRuler.setLocation(20, scrollArea.getOrigin().y);	
//				rulerNull.setLocation(rulerNull.getLocation().x, scrollArea.getOrigin().y);
//			}
//		};
			
//		scrollArea.getVerticalBar().addSelectionListener(listenerV);
		
		
		//Draw===================================
		
		_grid.addGrid(_drawArea);
		
		_drawArea.addPaintListener(new PaintListener() {
			public void paintControl(PaintEvent e) {
				
				if(_bgStatus == DrawArea.BACKGROUND_IMAGE && _showBGImage){
					
					e.gc.drawImage(_bgImage, _imageLocation.x, _imageLocation.y);
				}
				_grid.paint(e);
				
				if (_ani != null)
					_ani.paint(e.gc);
				
//				_ncollector.paint(e);
				
				
			}
		});
		
		
		_ruler.drawRuler(_drawLayer, _drawArea);
		_ruler.setVisible(false);
	}
	
	private void initObjectData(){
//		_ncollector = new NodeCollector(_drawArea, _editor);
//		
//		//---Colors init
//		if(_objModel.getBorderColor() != null)
//			_ncollector.setBorderColor(_objModel.getBorderColor());
//		
//		if(_objModel.getConnectionColor() != null)
//			_ncollector.setConnectionColor(_objModel.getConnectionColor());
//		
//		if(_objModel.getFillColor() != null)
//			_ncollector.setFillColor(_objModel.getFillColor());
//		
//		if(_objModel.getSelectionColor() != null)
//			_ncollector.setSelectionColor(_objModel.getSelectionColor());
//		
//		if(_objModel.getPoleColor() != null){
//			_ncollector.setPoleColor(_objModel.getPoleColor());
//		}
//		
//		if(_objModel.getPoleSelectionColor() != null){
//			_ncollector.setPoleSelectionColor(_objModel.getPoleSelectionColor());
//		}
//		
//		//---Nodes init
//		if(_objModel.getNodeList() != null && _objModel.getNodeList().size() > 0){
//			_ncollector.setNodeList(_objModel.getNodeList());
//		}
//		
//		//---Pole init
//		if(_objModel.hasPole()){
//			_ncollector.setPole(_objModel.getPole());
//		}
		
		
	}


	
	

}
